<?php
namespace app\admin\controller;
use app\admin\model\Staff as StaffModel;
use function MongoDB\BSON\toJSON;
use think\Db;
use clt\Leftnav;
use think\facade\Request;
use think\Validate;
use app\admin\model\Statis;
use app\admin\model\Category;
use app\admin\model\Staff;
class Statistics extends Common
{
    //统计列表
    public function index(){

            $year = input('year',date('Y'));
            $m = input('month',date('m'));
            $yid = input('post.yid');
            $tid = input('post.tid');
            $map='is_show=1';
            if ($m){
                $start =date('Y-m-01', strtotime(date("$year-$m-d")));
                $end =  date('Y-m-d', strtotime("$start +1 month -1 day"));
                $map .=" and day >='{$start}' and day<='{$end}'";
            }
            $model = new Staff();
            if(Request::isAjax()){

                $val=input('val');

                $url['val'] = $val;
                $this->assign('testval',$val);
                
                if ($yid){
                    $map .= " and yid=".$yid;
                }
                if ($tid){
                    $map .= " and tid=".$tid;
                }
                $list = Statis::where($map)->select();
                $data = array();
                $my = 0;
                $hx = 0;
                $bmy = 0;
                if ($list){
                    foreach ($list as $k=>$v){

                        $key = intval(date('d',strtotime($v['day'])));
                        if (empty($data[$v['yid']])){
                            $fwy = $model->getInfo($v['yid']);
                            if ($fwy){
                                $data[$v['yid']]['name'] = $fwy['name'];
                            }
                        }
                        if ($v['fid'] == 46){
                            $data[$v['yid']][$key]['M']++;
                        }elseif($v['fid'] == 47){
                            $data[$v['yid']][$key]['H']++;
                        }elseif ($v['fid'] == 48){
                            $data[$v['yid']][$key]['B']++;
                        }
                        if ($v['cid'] == 56){
                            $data[$v['yid']][$key]['CM']++;
                        }elseif($v['cid'] == 57){
                            $data[$v['yid']][$key]['CH']++;
                        }elseif ($v['cid'] == 58){
                            $data[$v['yid']][$key]['CB']++;
                        }
                    }
                    $total = intval(date('d',strtotime($end)))+1;
                    $lists = array();
                    foreach ($data as $kd=>$vd){
                        for ($i=1;$i<$total;$i++){
                            $lists[$kd]['name'] = $vd['name'];
                            $filed = "floder_".$i;
                            $lists[$kd][$filed] = "<span class='green'>".intval($vd[$i]['M'])."M</span><br><span style='color: #FFB800;'>".intval($vd[$i]['H'])."H</span><br><span class='red'>".intval($vd[$i]['B'])."B</span>";
                            $my += intval($vd[$i]['M']);
                            $hx += intval($vd[$i]['H']);
                            $bmy += intval($vd[$i]['B']);
//                            if ($vd[$filed]){
//                                $lists[$kd][$filed] = "<span class='green'>".intval($vd[$i]['M'])."M</span><br><span class='yellow'>".intval($vd[$i]['H'])."H</span><br><span class='red'>".intval($vd[$i]['B'])."B</span>";
//                            }else{
//                                $lists[$kd][$filed] = "<span class='green'>0M</span><br><span class='yellow'>0H</span><br><span class='red'>0B</span>";
//                            }
                        }
                    }
                }
                return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$lists,'count'=>count($lists),'my'=>$my,'hx'=>$hx,'bmy'=>$bmy];
            }
        $param = array();
        $total = intval(date('d',strtotime($end)))+1;
        $param[0]['field'] = 'name';
        $param[0]['title'] = "姓名\日期";
        $param[0]['width'] = "120";
        for($i=1;$i<=31;$i++){
            $param[$i]['field'] = 'floder_'.$i;
            $param[$i]['title'] = $i;
            $param[$i]['width'] = "85";
        }
        $worker = $model->where('is_show=1')->select();
        $tables = Category::where("pid=34")->select();
        $years = [];
        for ($i=0; $i <10 ; $i++) { 
            $year = date('Y',strtotime("-$i year"));
            $years[] = $year;
        }
        $this->assign('years',$years);
        $this->assign('total',$total);
        $this->assign('param',$param);
        $this->assign('worker',$worker);
        $this->assign('tables',$tables);
        $this->assign('m',date('m'));
        return $this->fetch('index');
    }
    /**
     * 单个员工详情表
     *
     */
    public function staffList(){
        if(Request::isAjax()){

            $map='is_show=1';
            $fid = input('post.fid');
            $cid = input('post.cid');
            $start = input('post.start');
            $end = input('post.end');

            if ($fid){
                $map .=" and fid=".$fid;
            }
            if ($cid){
                $map .=" and cid=".$cid;
            }
            if ($start){
                $map.=" and created_at>='{$start}'";
            }
            if ($end){
                $map.=" and created_at <='{$end} 23:59:59'";
            }
            $list = Statis::where($map)->select();
            $data = array();
            $cModel = new Category();
            if ($list){
                foreach ($list as $k=>$v){
                    $data[$k]['id'] = $v['id'];
                    $data[$k]['cdesc'] = $v['cdesc'];
                    $data[$k]['fdesc'] = $v['fdesc'];
                    $data[$k]['day'] = $v['created_at'];
                    $fwpj = $cModel->getInfo($v['fid']);
                    $data[$k]['fwpj'] = $fwpj['title'];
                    $table = $cModel->getInfo($v['tid']);
                    $data[$k]['table'] = $table['title'];
                    $data[$k]['yid'] = $v['yid'];
                    if ($v['ftag']){
                        $ftag = $cModel->where("id in({$v['ftag']})")->select();
                        $ftagstr = "";
                        foreach ($ftag as $kf=>$vf){
                            $ftagstr .= $vf['title'].",";
                        }
                        $data[$k]['ftag'] = rtrim($ftagstr,',');
                    }else{
                        $data[$k]['ftag'] = "--";
                    }
                    //菜品
                    $cppj = $cModel->getInfo($v['cid']);
                    $data[$k]['cppj'] = $cppj['title'];

                    if ($v['ctag']){
                        $ctag = $cModel->where("id in({$v['ctag']})")->select();
                        $ctagstr = "";
                        foreach ($ctag as $kc=>$vc){
                            $ctagstr .= $vc['title'].",";
                        }
                        $data[$k]['ctag'] = rtrim($ctagstr,',');
                    }else{
                        $data[$k]['ctag'] = "--";
                    }
                }
            }

            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$data,'rel'=>1];
        }
        $fwlist = Category::where('pid=45')->select();
        $this->assign('fwlist',$fwlist);
        $cplist = Category::where("pid=59")->select();
        $this->assign('cplist',$cplist);
        return view();
    }

    //更新管理员信息
    public function staffEdit(){
        if(request()->isPost()){
            $data = input('post.');
            $where['id'] = $data['id'];
            $param[$data['field']] = $data['des'];
            $param['author'] = session('aid');
            $param['updated_at'] = date('Y-m-d H:i:s');
            $res = Statis::update($param,$where);
            if ($res){
                return 1;
            }else{
                return 0;
            }
            return $result = ['code'=>1,'msg'=>'员工修改成功!','url'=>url('staffList')];
        }else{
            return 0;
        }
    }
}